﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CCL.Utility;

namespace Server.Service
{
    /// <summary>
    /// 로그인 요청에 대한 서비스를 제공합니다.
    /// </summary>
    public class Login : IService
    {
        /// <summary>
        /// 요청받은 E-Mail을 가져옵니다.
        /// </summary>
        public string Email { get; private set; }

        /// <summary>
        /// 요청받은 비밀번호를 가져옵니다.
        /// </summary>
        public string Password { get; private set; }

        /// <summary>
        /// 새로운 서비스를 생성합니다.
        /// </summary>
        public Login(char[] email, char[] password)
        {
            Email = StringUtility.ConvertToStr(email);
            Password = StringUtility.ConvertToStr(password);
        }
        
        /// <summary>
        /// 서비스를 시작합니다.
        /// </summary>
        /// <returns>전송할 byte배열</returns>
        public byte[] Execute()
        {
            byte[] returnBytes = null;
            using (SqlConnection conn = new SqlConnection(Properties.Resources.ConnectionString))
            {
                conn.Open();

                //SqlCommand comm = new SqlCommand(@"SELECT * FROM [User] WHERE email = '"+Email+"'", conn);
                SqlCommand comm = new SqlCommand(@"SELECT * FROM [User] WHERE email = @email", conn);
                comm.Parameters.AddWithValue("@email", Email);

                SqlDataReader reader = comm.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read();
                    string password = (string)reader["password"];

                    if (password == Password)
                    {
                        int userID = (int)reader["userId"];
                        string email = (string)reader["email"];
                        string name = (string)reader["name"];
                        DateTime dateTime = (DateTime)reader["joinedDateTime"];

                        CCL.SocketMessage.Response.Login login = new CCL.SocketMessage.Response.Login(userID, name, email, password, dateTime);
                        CCL.SocketMessage.Message msg = CCL.SocketMessage.Message.MessageResponseBuilder(login);
                        returnBytes = msg.GetByte();
                    }
                    else
                    {
                        CCL.SocketMessage.Response.Login login = new CCL.SocketMessage.Response.Login("비밀번호가 틀렸습니다.");
                        CCL.SocketMessage.Message msg = CCL.SocketMessage.Message.MessageResponseBuilder(login);
                        returnBytes = msg.GetByte();
                    }
                }
                else
                {
                    CCL.SocketMessage.Response.Login login = new CCL.SocketMessage.Response.Login("일치하는 정보가 없습니다.");
                    CCL.SocketMessage.Message msg = CCL.SocketMessage.Message.MessageResponseBuilder(login);
                    returnBytes = msg.GetByte();
                }
            }

            return returnBytes;
        }
    }
}
